package com.xdf.zipperdye.service;

import com.xdf.zipperdye.dao.business.CpMainMapper;
import com.xdf.zipperdye.model.CpMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CpMainServiceImpl implements CpMainService {
    @Autowired
    private CpMainMapper dao;
    @Autowired
    private BillCodeService billCodeService;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public CpMain insert(CpMain record) {
        record = setBillType(record);
        int ret = dao.insert(record);
        return record;
    }

    @Override
    public CpMain insertSelective(CpMain record) {
        record = setBillType(record);
        int ret = dao.insertSelective(record);
        return record;
    }

    @Override
    public CpMain selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public int updateByPrimaryKeySelective(CpMain record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CpMain record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public CpMain selectByBillcode(String billCode) {
        return dao.selectByBillcode(billCode);
    }

    private CpMain setBillType(CpMain main){
        String billType = Optional.ofNullable(main.getBill_type()).orElse("");
        String billCode = billCodeService.getNewBillCode(billType);
        main.setBill_code(billCode);
        return main;
    }
}
